/**
 * 文件名：BigDecimalUtil.java
 *
 * 创建人：虞云波 - yuyunbo@lawxp.com
 *
 * 创建时间：2015-11-23 下午12:01:52
 *
 * 版权所有：南京汇法正信信息科技有限公司
 */
package com.cbwl.eoas.common.util;

import java.math.BigDecimal;


/**
 * <p>[描述信息：BigDecimal工具类]</p>
 *
 * @author 虞云波 - yuyunbo@lawxp.com
 * @version 1.0 Created on 2015-11-23 下午12:01:52
 */
public class BigDecimalUtil {

	/**
	 * 
	 * <p>加法</p>
	 * 
	 * @param first
	 * @param second
	 * @param percision,精度
	 * @return
	 * @author: 虞云波 - yuyunbo@lawxp.com
	 * @date: Created on 2015-11-23 下午01:22:55
	 */
	public static BigDecimal add(Object first,Object second,int percision){
		
		String firstStr = String.valueOf(first);
		String secondStr = String.valueOf(second);
		
		return new BigDecimal(firstStr).add(new BigDecimal(secondStr)).setScale(percision, BigDecimal.ROUND_HALF_UP);
	}
	
	/**
	 * 
	 * <p>减法</p>
	 * 
	 * @param first   减数
	 * @param second  被减数
	 * @param percision,精度
	 * @return
	 * @author: 虞云波 - yuyunbo@lawxp.com
	 * @date: Created on 2015-11-23 下午01:26:39
	 */
   public static BigDecimal sub(Object first,Object second,int percision){
		
		String firstStr = String.valueOf(first);
		String secondStr = String.valueOf(second);
		
		return new BigDecimal(firstStr).subtract(new BigDecimal(secondStr)).setScale(percision, BigDecimal.ROUND_HALF_UP);
	}
   
   /**
    * 
    * <p>乘法</p>
    * 
    * @param first
    * @param second
    * @param percision,精度
    * @return
    * @author: 虞云波 - yuyunbo@lawxp.com
    * @date: Created on 2015-11-23 下午01:27:27
    */
   public static BigDecimal mul(Object first,Object second,int percision){
		
		String firstStr = String.valueOf(first);
		String secondStr = String.valueOf(second);
		
		return new BigDecimal(firstStr).multiply(new BigDecimal(secondStr)).setScale(percision, BigDecimal.ROUND_HALF_UP);
	}
   
   /**
    * 
    * <p>除法</p>
    * 
    * @param first 除数
    * @param second  被除数
    * @param percision,精度
    * @return
    * @author: 虞云波 - yuyunbo@lawxp.com
    * @date: Created on 2015-11-23 下午01:28:04
    */
   public static BigDecimal div(Object first,Object second,int percision){
		
		String firstStr = String.valueOf(first);
		String secondStr = String.valueOf(second);
		
		return new BigDecimal(firstStr).divide(new BigDecimal(secondStr),percision,BigDecimal.ROUND_HALF_UP);
	}  
   
   /**
    * 
    * <p>对数字处理，有小数点保留两位数字</p>
    * 
    * @param value
    * @param percision
    * @return
    * @author: 虞云波 - yuyunbo@lawxp.com
    * @date: Created on 2016-6-13 下午04:44:32
    */
   public static String perGet(Object value,int percision){
		String valueStr = String.valueOf(value);
		if(valueStr.contains(".")){
			return new BigDecimal(valueStr).setScale(percision, BigDecimal.ROUND_HALF_UP).toString();
		}else{
			return new BigDecimal(valueStr).setScale(0, BigDecimal.ROUND_HALF_UP).toString();
		}
	}  
   
   
   
   public static void main(String[] args) {	   
      System.out.println(perGet("18512540145",2));
      System.out.println(new BigDecimal(50).compareTo(new BigDecimal(49.99)));
   }
  
}
